package com.yh.workbill.ui.editworkbill.mvp;

import android.content.ComponentName;

import com.yh.workbill.App;
import com.yh.workbill.db.Company;
import com.yh.workbill.db.CompanyDao;
import com.yh.workbill.db.UnitCompany;
import com.yh.workbill.db.UnitCompanyDao;
import com.yh.workbill.model.WorkBill;
import com.yh.workbill.model.WorkDevices;
import com.yh.workbill.ui.addworkbill.mvp.AddWorkBill_Model;
import com.yh.workbill.ui.companyInfo.mvp.AddCompanyInfo_Model;
import com.yh.workbill.ui.companyInfo.mvp.AddCompanyInfo_Presenter;
import com.yh.workbill.ui.leaseInfo.mvp.AddLease_Model;

import java.util.List;

import javax.crypto.interfaces.PBEKey;

import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

public class EditWork_Presenter implements LifecycleObserver {
    private EditWork_View view;
    private EditWork_Model model;

    public EditWork_Presenter(EditWork_View view) {
        this.view = view;
        this.model = new EditWork_Model();
    }

    public void getDevices() {
        view.showDialog("正在加载数据", true);
        model.getAllDevices(new AddWorkBill_Model.DevicesBack() {
            @Override
            public void Suceess(List<WorkDevices> data) {
                view.hideDialog();
                view.getAllDevices(data);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.showTasot(msg);
            }
        });
    }

    public void del(String uuid) {
        view.showDialog("正在删除数据中...", false);
        model.delWorkBill(uuid, new EditWork_Model.EditWorkBillCallBack() {
            @Override
            public void onSuccess(String msg) {
                view.hideDialog();
                view.onDelSuccess(uuid);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.onFail(msg);
            }
        });
    }

    public void addCompany(String company_name, String company_address, String company_phone) {
        model.addCompanyInfo(company_name, company_address, company_phone, new AddCompanyInfo_Model.AddCallBack() {
            @Override
            public void onSuccess(String company, String company_address, String company_phone) {
                ArchTaskExecutor.getIOThreadExecutor().execute(() -> {
                    CompanyDao companyDao = App.appDataBase.getCompanyDao();
                    companyDao.add(new Company(company, company_address, company_phone));
                });
            }

            @Override
            public void fail(String msg) {

            }
        });
    }

    public void addUnit(String work_use_company) {
        model.addUnitName(work_use_company, new AddLease_Model.AddLeaseCallback() {
            @Override
            public void onSuccess(String name) {
                ArchTaskExecutor.getIOThreadExecutor().execute(new Runnable() {
                    @Override
                    public void run() {
                        UnitCompanyDao unitCompanyDao = App.appDataBase.getUnitCompanyDao();
                        unitCompanyDao.add(new UnitCompany(name));
                    }
                });
            }

            @Override
            public void fail(String msg) {
            }
        });
    }


    public void update(WorkBill workBill) {
        view.showDialog("正在更新数据中...", false);
        model.updataWorkBill(workBill, new EditWork_Model.EditWorkBillCallBack() {
            @Override
            public void onSuccess(String msg) {
                view.hideDialog();
                view.OnUpdateSuccess(workBill);
            }

            @Override
            public void fail(String msg) {
                view.hideDialog();
                view.onFail(msg);
            }
        });
    }


    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void ondestory() {
        if (this.view != null) {
            this.view = null;
        }
    }
}
